package v1

import (
	"framework-server/model"
	"framework-server/model/request"
	"framework-server/model/response"
	"framework-server/service"
	"fmt"
	"github.com/gin-gonic/gin"
)

func GetSysButtonList(c *gin.Context) {
	// 此结构体仅本方法使用
	var info request.GetSysButtonListRequest
	_ = c.ShouldBindQuery(&info)
	err, list, total := service.GetSysButtonList(info)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("获取数据失败，%v", err), c)
	} else {
		response.OkWithData(response.PageResult{
			List:     list,
			Total:    total,
			Page:     info.PageInfo.Page,
			PageSize: info.PageInfo.PageSize,
		}, c)
	}
}


func FindSysButton(c *gin.Context) {
	var info model.SysButton
	_ = c.ShouldBindQuery(&info)
	err, resysButton := service.GetSysButton(info.ID)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("查询失败，%v", err), c)
	} else {
		response.OkWithData(gin.H{"resysButton": resysButton}, c)
	}
}

func CreateSysButton(c *gin.Context) {
	var info model.SysButton
	_ = c.ShouldBindJSON(&info)
	err := service.CreateSysButton(info)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("创建失败，%v", err), c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

func UpdateSysButton(c *gin.Context) {
	var info model.SysButton
	_ = c.ShouldBindJSON(&info)
	err := service.UpdateSysButton(info)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("修改数据失败，%v", err), c)
	} else {
		response.OkWithMessage("修改数据成功", c)
	}
}

func DeleteSysButton(c *gin.Context) {
	var info model.SysButton
	_ = c.ShouldBindJSON(&info)
	err := service.DeleteSysButton(info)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("删除数据失败，%v", err), c)
	} else {
		response.OkWithMessage("删除数据成功", c)
	}
}


func GetAllButtons(c *gin.Context) {
	var info request.GetAllButtonsRequest
	_ = c.ShouldBindJSON(&info)
	err, buttons := service.GetAllButtons(info)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("删除数据失败，%v", err), c)
	} else {
		response.OkWithData(map[string]interface{}{
			"buttons": buttons,
		}, c)
	}
}

func GetButtonByAutorityId(c *gin.Context) {
	var info request.GetButtonByAutorityIdReqeust
	_ = c.ShouldBindJSON(&info)
	err, buttons := service.GetButtonByAutorityId(info.AuthorityId)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("删除数据失败，%v", err), c)
	} else {
		response.OkWithData(map[string]interface{}{
			"buttons": buttons,
		}, c)
	}
}

func AddButtonAuthority(c *gin.Context) {
	var info request.AddButtonAuthorityReqeust
	_ = c.ShouldBindJSON(&info)
	err := service.AddButtonAuthority(info)
	if err != nil {
		response.FailWithMessage(fmt.Sprintf("更新数据失败，%v", err), c)
	} else {
		response.OkWithMessage("更新数据成功", c)
	}
}